﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2009 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2009-2-6 16:13:51
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL
{
    /// <summary>
    /// 数据访问类 IntegralWasteBook
    /// </summary>
    public class IntegralWasteBook : IIntegralWasteBook
    {
        public IntegralWasteBook()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(Model.IntegralWasteBook model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [IntegralWasteBook](");
            strSql.Append("[IntergalId],[IntergalType],[IntergalName],[IntergalRange],[IntergalDateTime],[IntergalExtendAttribute1],[IntergalExtendAttribute2],[IntergalExtendAttribute3],[IntergalExtendAttribute4],[IntergalExtendAttribute5],[IntergalExtendAttribute6],[IntergalExtendAttribute7],[IntergalExtendAttribute8])");
            strSql.Append(" values (");
            strSql.Append("@IntergalId,@IntergalType,@IntergalName,@IntergalRange,@IntergalDateTime,@IntergalExtendAttribute1,@IntergalExtendAttribute2,@IntergalExtendAttribute3,@IntergalExtendAttribute4,@IntergalExtendAttribute5,@IntergalExtendAttribute6,@IntergalExtendAttribute7,@IntergalExtendAttribute8)");
            SqlParameter[] parameters = {
                    new SqlParameter("@IntergalId", SqlDbType.VarChar,24),
                    new SqlParameter("@IntergalType", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalName", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalRange", SqlDbType.Decimal),
                    new SqlParameter("@IntergalDateTime", SqlDbType.DateTime),
                    new SqlParameter("@IntergalExtendAttribute1", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalExtendAttribute2", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalExtendAttribute3", SqlDbType.VarChar,100),
                    new SqlParameter("@IntergalExtendAttribute4", SqlDbType.VarChar,100),
                    new SqlParameter("@IntergalExtendAttribute5", SqlDbType.Int),
                    new SqlParameter("@IntergalExtendAttribute6", SqlDbType.Int),
                    new SqlParameter("@IntergalExtendAttribute7", SqlDbType.Decimal),
                    new SqlParameter("@IntergalExtendAttribute8", SqlDbType.Decimal)
            };
            parameters[0].Value = model.IntergalId;

            if (model.IntergalType != null)
                parameters[1].Value = model.IntergalType;
            else
                parameters[1].Value = DBNull.Value;


            if (model.IntergalName != null)
                parameters[2].Value = model.IntergalName;
            else
                parameters[2].Value = DBNull.Value;

            parameters[3].Value = model.IntergalRange;

            if (model.IntergalDateTime != DateTime.MinValue)
                parameters[4].Value = model.IntergalDateTime;
            else
                parameters[4].Value = DBNull.Value;


            if (model.IntergalExtendAttribute1 != null)
                parameters[5].Value = model.IntergalExtendAttribute1;
            else
                parameters[5].Value = DBNull.Value;


            if (model.IntergalExtendAttribute2 != null)
                parameters[6].Value = model.IntergalExtendAttribute2;
            else
                parameters[6].Value = DBNull.Value;


            if (model.IntergalExtendAttribute3 != null)
                parameters[7].Value = model.IntergalExtendAttribute3;
            else
                parameters[7].Value = DBNull.Value;


            if (model.IntergalExtendAttribute4 != null)
                parameters[8].Value = model.IntergalExtendAttribute4;
            else
                parameters[8].Value = DBNull.Value;

            parameters[9].Value = model.IntergalExtendAttribute5;
            parameters[10].Value = model.IntergalExtendAttribute6;
            parameters[11].Value = model.IntergalExtendAttribute7;
            parameters[12].Value = model.IntergalExtendAttribute8;

            if (SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0)
                return model.IntergalId;
            else
                return null;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.IntegralWasteBook model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [IntegralWasteBook] set ");
            strSql.Append("[IntergalType]=@IntergalType,");
            strSql.Append("[IntergalName]=@IntergalName,");
            strSql.Append("[IntergalRange]=@IntergalRange,");
            strSql.Append("[IntergalDateTime]=@IntergalDateTime,");
            strSql.Append("[IntergalExtendAttribute1]=@IntergalExtendAttribute1,");
            strSql.Append("[IntergalExtendAttribute2]=@IntergalExtendAttribute2,");
            strSql.Append("[IntergalExtendAttribute3]=@IntergalExtendAttribute3,");
            strSql.Append("[IntergalExtendAttribute4]=@IntergalExtendAttribute4,");
            strSql.Append("[IntergalExtendAttribute5]=@IntergalExtendAttribute5,");
            strSql.Append("[IntergalExtendAttribute6]=@IntergalExtendAttribute6,");
            strSql.Append("[IntergalExtendAttribute7]=@IntergalExtendAttribute7,");
            strSql.Append("[IntergalExtendAttribute8]=@IntergalExtendAttribute8");
            strSql.Append(" where [IntergalId]=@IntergalId");
            SqlParameter[] parameters = {
                    new SqlParameter("@IntergalId", SqlDbType.VarChar,24),
                    new SqlParameter("@IntergalType", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalName", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalRange", SqlDbType.Decimal),
                    new SqlParameter("@IntergalDateTime", SqlDbType.DateTime),
                    new SqlParameter("@IntergalExtendAttribute1", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalExtendAttribute2", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalExtendAttribute3", SqlDbType.VarChar,100),
                    new SqlParameter("@IntergalExtendAttribute4", SqlDbType.VarChar,100),
                    new SqlParameter("@IntergalExtendAttribute5", SqlDbType.Int),
                    new SqlParameter("@IntergalExtendAttribute6", SqlDbType.Int),
                    new SqlParameter("@IntergalExtendAttribute7", SqlDbType.Decimal),
                    new SqlParameter("@IntergalExtendAttribute8", SqlDbType.Decimal)
            };
            parameters[0].Value = model.IntergalId;

            if (model.IntergalType != null)
                parameters[1].Value = model.IntergalType;
            else
                parameters[1].Value = DBNull.Value;


            if (model.IntergalName != null)
                parameters[2].Value = model.IntergalName;
            else
                parameters[2].Value = DBNull.Value;

            parameters[3].Value = model.IntergalRange;

            if (model.IntergalDateTime != DateTime.MinValue)
                parameters[4].Value = model.IntergalDateTime;
            else
                parameters[4].Value = DBNull.Value;


            if (model.IntergalExtendAttribute1 != null)
                parameters[5].Value = model.IntergalExtendAttribute1;
            else
                parameters[5].Value = DBNull.Value;


            if (model.IntergalExtendAttribute2 != null)
                parameters[6].Value = model.IntergalExtendAttribute2;
            else
                parameters[6].Value = DBNull.Value;


            if (model.IntergalExtendAttribute3 != null)
                parameters[7].Value = model.IntergalExtendAttribute3;
            else
                parameters[7].Value = DBNull.Value;


            if (model.IntergalExtendAttribute4 != null)
                parameters[8].Value = model.IntergalExtendAttribute4;
            else
                parameters[8].Value = DBNull.Value;

            parameters[9].Value = model.IntergalExtendAttribute5;
            parameters[10].Value = model.IntergalExtendAttribute6;
            parameters[11].Value = model.IntergalExtendAttribute7;
            parameters[12].Value = model.IntergalExtendAttribute8;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string IntergalId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete [IntegralWasteBook] ");
            strSql.Append(" where [IntergalId]=@IntergalId");
            SqlParameter[] parameters = {
                    new SqlParameter("@IntergalId", SqlDbType.VarChar,24)};
            parameters[0].Value = IntergalId;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string IntergalId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [IntegralWasteBook]");
            strSql.Append(" where [IntergalId]= @IntergalId");
            SqlParameter[] parameters = {
                    new SqlParameter("@IntergalId", SqlDbType.VarChar,24)};
            parameters[0].Value = IntergalId;
            return SqlHelper.Exists(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.IntegralWasteBook GetModel(string IntergalId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [IntegralWasteBook] ");
            strSql.Append(" where [IntergalId]=@IntergalId");
            SqlParameter[] parameters = {
                    new SqlParameter("@IntergalId", SqlDbType.VarChar,24)};
            parameters[0].Value = IntergalId;
            DataSet ds = SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取数据集,建议只在多表联查时使用
        /// </summary>
        public DataSet GetDataSet(string strSql)
        {
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql);
        }

        /// <summary>
        /// 获取泛型数据列表,建议只在多表联查时使用
        /// </summary>
        public IList<Model.IntegralWasteBook> GetList(string strSql)
        {
            return GetList(GetDataSet(strSql));
        }

        /// <summary>
        /// 获取数据集,在单表查询时使用
        /// </summary>
        public DataSet GetDataSet(string strWhat, string strWhere, string strOrderby)
        {
            if (string.IsNullOrEmpty(strWhat))
                strWhat = "*";
            StringBuilder strSql = new StringBuilder("select " + strWhat + " from [IntegralWasteBook]");
            if (!string.IsNullOrEmpty(strWhere))
                strSql.Append(" where " + strWhere);
            if (!string.IsNullOrEmpty(strOrderby))
                strSql.Append(" order by " + strOrderby);
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString());
        }

        /// <summary>
        /// 获取泛型数据列表,在单表查询时使用
        /// </summary>
        public IList<Model.IntegralWasteBook> GetList(string strWhat, string strWhere, string strOrderby)
        {
            return GetList(GetDataSet(strWhat, strWhere, strOrderby));
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.IntegralWasteBook> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[IntegralWasteBook]", null, pageSize, pageIndex, fldSort, sort, strCondition, "IntergalId", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.IntegralWasteBook GetModel(DataRow r)
        {
            Model.IntegralWasteBook model = new Model.IntegralWasteBook();
            model.IntergalId = SqlHelper.GetString(r["IntergalId"]);
            model.IntergalType = SqlHelper.GetString(r["IntergalType"]);
            model.IntergalName = SqlHelper.GetString(r["IntergalName"]);
            model.IntergalRange = SqlHelper.GetDecimal(r["IntergalRange"]);
            model.IntergalDateTime = SqlHelper.GetDateTime(r["IntergalDateTime"]);
            model.IntergalExtendAttribute1 = SqlHelper.GetString(r["IntergalExtendAttribute1"]);
            model.IntergalExtendAttribute2 = SqlHelper.GetString(r["IntergalExtendAttribute2"]);
            model.IntergalExtendAttribute3 = SqlHelper.GetString(r["IntergalExtendAttribute3"]);
            model.IntergalExtendAttribute4 = SqlHelper.GetString(r["IntergalExtendAttribute4"]);
            model.IntergalExtendAttribute5 = SqlHelper.GetInt(r["IntergalExtendAttribute5"]);
            model.IntergalExtendAttribute6 = SqlHelper.GetInt(r["IntergalExtendAttribute6"]);
            model.IntergalExtendAttribute7 = SqlHelper.GetDecimal(r["IntergalExtendAttribute7"]);
            model.IntergalExtendAttribute8 = SqlHelper.GetDecimal(r["IntergalExtendAttribute8"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.IntegralWasteBook> GetList(DataSet ds)
        {
            List<Model.IntegralWasteBook> l = new List<Model.IntegralWasteBook>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
